library(leaflet)
library(dplyr)
library(RColorBrewer)
library(sf)leaflet tutorial
library
Basic map and markers
leaflet() |>
addTiles() |>
setView(lng = 121.774, lat = 12.8797, zoom = 6) |>
addMarkers(lng = 120.9842, lat = 14.5995, popup = "Manila") |>
addMarkers(lng = 125.5017, lat = 8.4753, popup = "Butuan City") |>
addMarkers(lat = 15.671818823094426, lng = 120.89067531956579, popup = "PhilRice")🧠 Exercise 1:
Add a marker for your house and the house of you
Add popups with city names
Try adjusting the zoom level
2. 🌟 Customizing Markers
Demo
customIcon <- makeIcon(
iconUrl ="https://raw.githubusercontent.com/nmfrancisco14/phl_maps/master/DAC - LOGO OFFICIAL.png",
iconWidth = 100,
iconHeight = 100 )
leaflet() |>
addTiles() |>
setView(lng = 121.774, lat = 12.8797, zoom = 6) |>
addMarkers(lat = 15.671818823094426,
lng = 120.89067531956579,
icon = customIcon, popup = "Data Analytics Center") 🧠 Exercise 2:
Use a different PNG icon (e.g. from Wikimedia or GitHub) and place it in Cebu or Davao.
What happens if you change the icon size?
Try using different shapes or logos.
3. 🧭 Polygons & Polylines
Load boundaries:
ph_province <- st_read(
"https://raw.githubusercontent.com/nmfrancisco14/phl_maps/master/phl_provinces_ncr-districts_icc.geojson")Reading layer `phl_provinces_ncr-districts_icc' from data source
`https://raw.githubusercontent.com/nmfrancisco14/phl_maps/master/phl_provinces_ncr-districts_icc.geojson'
using driver `GeoJSON'
Simple feature collection with 88 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 116.9283 ymin: 4.58694 xmax: 126.6053 ymax: 21.07014
Geodetic CRS: WGS 84
Demo: Add polygon boundaries
leaflet(ph_province) |>
addTiles() |>
addPolygons(fillColor = "lightblue",
fillOpacity = 0.5,
color = "black",
weight = 1)